#include "ssicloud_survey_select.h"


ssicloud::SurveySelectQuery::SurveySelectQuery()
    : ListObject<SurveySelectQueryLNode>(LQ_BASE_URL "/survey/selectApp"
                           , OBJECT_NAME_SurveySelectQuery
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ssicloud::SurveySelectQuery::requestAll(int pageNum, int pageSize)
{
    snprintf(m_body, sizeof(m_body),
             "{\"vin\": \"%s\","
             "\"pageNum\": %d,"
             "\"pageSize\": %d"
             "}", VIN_STRING, pageNum, pageSize);
    mUrl.appendBody(m_body);
    return obtain();
}

void ssicloud::SurveySelectQuery::profile()
{
    ListTable<SurveySelectQueryLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------问卷调查 - 分页查询-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "标题:%s, 描述:%s, 创建时间:%s",
                    it->surveyTitle.string(), it->surveyDes.c_str(), it->createTime.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::SurveySelectQuery::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        if (records) {
            for (cJSON *item = records->child; NULL != item; item = item->next )
            {
                SurveySelectQueryLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, surveyTitle, tmp.surveyTitle);
                JSON_VALUETRING_SWAP_BYTESTRING(item, surveyDes, tmp.surveyDes);
                JSON_VALUETRING_SWAP_BYTESTRING(item, completed, tmp.completed);
                JSON_VALUETRING_SWAP_BYTESTRING(item, createTime, tmp.createTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, updateTime, tmp.updateTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, publishTime, tmp.publishTime);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ssicloud::SurveySelectDetail::SurveySelectDetail()
    : ListObject<SurveySelectDetailLNode>(LQ_BASE_URL "/survey/selectSurveyApp"
                           , OBJECT_NAME_SurveySelectDetail
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    // mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("vin", VIN_STRING);
}

bool ssicloud::SurveySelectDetail::requestAll(const char *id)
{
    if (isLoading()) return true;

    m_id = id;
    mUrl.appendChange("id", m_id.c_str());

    return obtain();
}

void ssicloud::SurveySelectDetail::profile()
{
    ListTable<SurveySelectDetailLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------问卷调查 - 详情-------");

    for (; it != mNodes.end(); ++it)
    {
          GEN_Printf(LOG_DUMP, "ID:%s, 问题:%s, 备注:%s",
                     it->id.string(), it->question.c_str(),
                     it->note.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::SurveySelectDetail::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    // GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *surveys = cJSON_GetObjectItem(dataJson, "surveys");
        if (surveys) {
            for (cJSON *item = surveys->child; NULL != item; item = item->next )
            {
                SurveySelectDetailLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
                JSON_VALUETRING_SWAP_BYTESTRING(item, question, tmp.question);
                JSON_VALUETRING_SWAP_BYTESTRING(item, note, tmp.note);
                JSON_VALUETRING_SWAP_BYTESTRING(item, must, tmp.must);

                cJSON *options = cJSON_GetObjectItem(dataJson, "options");
                if (options) {
                    SurveySelectDetailOptionLNode itmp;
                    JSON_VALUETRING_SWAP_BYTESTRING(item, optionNo, itmp.optionNo);
                    JSON_VALUETRING_SWAP_BYTESTRING(item, optionName, itmp.optionName);
                    JSON_VALUETRING_SWAP_BYTESTRING(item, choose, itmp.choose);
                    tmp.options.push_back(itmp);
                }

                GEN_Printf(LOG_DEBUG, "items size=%d", tmp.options.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}


ssicloud::SurveySubmit::SurveySubmit()
    : ItemObject<EmptyInfoNode>(LQ_BASE_URL "/survey/submit"
                           , OBJECT_NAME_SurveySubmit
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ssicloud::SurveySubmit::submit(const SFString &mainId, const SFString &beginTime, const ssicloud::SurveySubmit::AnswersList &answers)
{
    if (isLoading()) {
        GEN_Printf(LOG_WARN, "current is loading.");
        return false;
    }

    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "vin", VIN_STRING);
    cJSON_AddStringToObject(root, "mainId", mainId.c_str());
    cJSON_AddStringToObject(root, "beginTime", beginTime.c_str());

    cJSON *anslist = cJSON_AddArrayToObject(root, "answers");

    for (auto &ans : answers) {
        cJSON *event = cJSON_CreateObject();
        cJSON_AddStringToObject(event, "surveyId", ans.surveyId.c_str());

        cJSON *optionId = cJSON_AddArrayToObject(event, "optionId");
        for (auto &ids : ans.optionId) {
            cJSON_AddStringToObject(optionId, "", ids.c_str());
        }

        cJSON_AddItemToArray(anslist, event);
    }

    // char *out = cJSON_PrintUnformatted(root);
    // GEN_Printf(LOG_DEBUG, "%s", out);

    cJSON_PrintPreallocated(root, m_body, sizeof (m_body)-1, false);
    GEN_Printf(LOG_DEBUG, "%s", m_body);

    mUrl.appendBody(m_body);
    return obtain();
}

int ssicloud::SurveySubmit::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *code = cJSON_GetObjectItem(root, "code");
    if (code && *(code->valuestring) == '1')
    {
        // cJSON_GetStringValue()
        // GEN_Printf(LOG_DEBUG, "code value = %d, %s", (int)code->valuedouble);
        GEN_Printf(LOG_INFO, "upload info successfully.");
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void ssicloud::SurveySubmit::profile()
{

}
